﻿using Application_Core.Dispatcher_Component.Access_Layer;
using Application_Core.Dispatcher_Component.Access_Layer.Implementations;
using System;
using System.Collections.Generic;

namespace Client.Client_Component
{
    public class KundeServiceImpl : KundeService
    {        
        public void CreateKunde(KundeDTO kundeDTO)
        {
            DispatcherForClientService service = new DispatcherComponentFacade();
            service.RequestInvokeMethod("CreateKunde", "KundeService", new Type[] { typeof(KundeDTO) }, new Object[] { kundeDTO });
        }

        public void CreateOrUpdateKunde(KundeDTO kundeDTO)
        {
            DispatcherForClientService service = new DispatcherComponentFacade();
            service.RequestInvokeMethod("CreateOrUpdateKunde", "KundeService", new Type[] { typeof(KundeDTO) }, new Object[] { kundeDTO });
        }

        public KundeDTO FindKunde(int nummer)
        {
            DispatcherForClientService service = new DispatcherComponentFacade();
            Object kundeDTOObj = service.RequestInvokeMethod("FindKunde", "KundeService", 
                new Type[] { typeof(Int32) }, new Object[] { nummer });
            if (kundeDTOObj == null)
            {
                return null;
            }
            else if (kundeDTOObj.GetType() == typeof(KundeDTO))
            {
                return (KundeDTO)kundeDTOObj;
            }
            throw new WrongReturningTypeException("Der erhaltene Typ entspricht " + kundeDTOObj.GetType() + ", es wird jedoch der Typ KundeDTO oder 'null' erwartet.");
        }

        public List<KundeDTO> FindKunden(string nachname)
        {
            DispatcherForClientService service = new DispatcherComponentFacade();
            Object kundeDTOListObj = service.RequestInvokeMethod("FindKunden", "KundeService", 
                new Type[] { typeof(String) }, new Object[] { nachname });
            if (kundeDTOListObj.GetType() == typeof(List<KundeDTO>))
            {
                return (List<KundeDTO>)kundeDTOListObj;
            }
            throw new WrongReturningTypeException("Der erhaltene Typ entspricht " + kundeDTOListObj.GetType() + ", es wird jedoch der Typ List<KundeDTO> erwartet.");
        }

        public void DeleteKunde(int nummer)
        {
            DispatcherForClientService service = new DispatcherComponentFacade();
            service.RequestInvokeMethod("DeleteKunde", "KundeService", new Type[] { typeof(Int32) }, new Object[] { nummer });
        }
    }
}
